﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLHD.Models;

namespace QLHD
{
    public partial class KCPPopup : System.Web.UI.Page
    {
        public string maphong = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            int mahd = 0;
            if (Request.Params["MaHD"] != null)
            {
                mahd = int.Parse(Request.Params["MaHD"]);
            }
            QLHD2013Entities db = new QLHD2013Entities();

            //var kcps = db.KhoanChiPhis.Where(q => (q.MaHD == mahd) || (q.HopDong != null && q.HopDong.HopDong3 != null && q.HopDong.HopDong3.MaHD == mahd)).ToList();
            //List<TreeItem> List = new List<TreeItem>();
            //TreeItem tr = null;
            //foreach (var item in kcps.Where(q => q.ParentID == null).ToList())
            //{
            //    int Level = 0;
            //    tr = new TreeItem();
            //    tr.Name = Utilities.Utilities.DuplicateString("--", Level) + item.NoiDung;
            //    tr.Id = item.MaKCP.ToString();
            //    List.Add(tr);
            //    Utilities.Utilities.GetAllKCPTree(ref List, item.MaKCP, kcps, ref Level);
            //}
            //List.Insert(0, new TreeItem() { Id = "", Name = "Chọn khoán chi phí cha" });
            //drkcpcha.DataSource = List;
            //drkcpcha.DataBind();


            if (!string.IsNullOrEmpty(Request.Params["MaKCP"]))
            {
                int makcp = int.Parse(Request.Params["MaKCP"]);
                if (makcp > 0)
                {

                    KhoanChiPhi kcp = db.KhoanChiPhis.Where(q => q.MaKCP == makcp).FirstOrDefault();

                    if (kcp.HopDong != null && kcp.HopDong.HopDong3 != null && kcp.HopDong.HopDong3.DepartmentID != null)
                        maphong = kcp.HopDong.HopDong3.DepartmentID.ToString();

                    txtsokcp.Text = kcp.SoKCP;
                    txtgiatri.Value = kcp.GiaTri != null ? kcp.GiaTri.ToString() : "0";
                    txtNgay.Value = ((DateTime)kcp.Ngay).ToString("dd/MM/yyyy");
                    txtghichu.Value = kcp.GhiChu;
                    txtnoidung.Value = kcp.NoiDung;

                    txtgktttc.Value = kcp.CPGK_TTTC != null ? kcp.CPGK_TTTC.ToString() : "0";
                    txtgkquacty.Value = kcp.CPGK_QuaCTy != null ? kcp.CPGK_QuaCTy.ToString() : "0";
                    txtgktkcm.Value = kcp.CPGK_TKCM != null ? kcp.CPGK_TKCM.ToString() : "0";
                    txtgkthuechuyengia.Value = kcp.CPGK_ThueChuyenGia != null ? kcp.CPGK_ThueChuyenGia.ToString() : "0";
                    txtgkptmautn.Value = kcp.CPGK_PTMauTN != null ? kcp.CPGK_PTMauTN.ToString() : "0";
                    txtgkptmaumt.Value = kcp.CPGK_PTMauMT != null ? kcp.CPGK_PTMauMT.ToString() : "0";
                    txtgkhoachat.Value = kcp.CPGK_HoaChat != null ? kcp.CPGK_HoaChat.ToString() : "0";
                    txtgkluong.Value = kcp.CPGK_Luong != null ? kcp.CPGK_Luong.ToString() : "0";
                    txtgkchiphikhac.Value = kcp.CPGK_CPK != null ? kcp.CPGK_CPK.ToString() : "0";
                    txtgkthuemuangoaikhac.Value = kcp.CPGK_Thuemuangoai != null ? kcp.CPGK_Thuemuangoai.ToString() : "0";
                    txtgknguyenvl.Value = kcp.CPGK_NVL != null ? kcp.CPGK_NVL.ToString() : "0";

                    txtthtttc.Value = kcp.CPTH_TTTC != null ? kcp.CPTH_TTTC.ToString() : "0";
                    txtthquacty.Value = kcp.CPTH_QuaCty != null ? kcp.CPTH_QuaCty.ToString() : "0";
                    txtthtkcm.Value = kcp.CPTH_TKCM != null ? kcp.CPTH_TKCM.ToString() : "0";
                    txtththuechuyengia.Value = kcp.CPTH_ThueChuyenGia != null ? kcp.CPTH_ThueChuyenGia.ToString() : "0";
                    txtthptmautn.Value = kcp.CPTH_PTMauTN != null ? kcp.CPTH_PTMauTN.ToString() : "0";
                    txtthptmaumt.Value = kcp.CPTH_PTMauMT != null ? kcp.CPTH_PTMauMT.ToString() : "0";
                    txtthhoachat.Value = kcp.CPTH_HoaChat != null ? kcp.CPTH_HoaChat.ToString() : "0";
                    txtthchiphikhac.Value = kcp.CPTH_CPK != null ? kcp.CPTH_CPK.ToString() : "0";
                    txtthluong.Value = kcp.CPTH_Luong != null ? kcp.CPTH_Luong.ToString() : "0";

                    txtththuemuangoaikhac.Value = kcp.CPTH_Thuemuangoai != null ? kcp.CPTH_Thuemuangoai.ToString() : "0";
                    txtthnguyenvl.Value = kcp.CPTH_NVL != null ? kcp.CPTH_NVL.ToString() : "0";

                    //if (kcp.ParentID != null)
                    //{
                    //    var lstitem = drkcpcha.Items.FindByValue(kcp.ParentID.ToString());
                    //    if (lstitem != null)
                    //    {
                    //        drkcpcha.ClearSelection();
                    //        lstitem.Selected = true;
                    //    }
                    //}
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int mahd = 0;
            if (!string.IsNullOrEmpty(Request.Params["MaHD"]))
            {
                mahd = int.Parse(Request.Params["MaHD"]);
            }
            int makcp = 0;
            if (!string.IsNullOrEmpty(Request.Params["MaKCP"]))
            {
                makcp = int.Parse(Request.Params["MaKCP"]);
            }
            int parentid = 0;
            if (!string.IsNullOrEmpty(Request.Params["ParentID"]))
            {
                parentid = int.Parse(Request.Params["ParentID"]);
            }
            string noidung = txtnoidung.Value;
            string sokcp = txtsokcp.Text;
            string giatri = txtgiatri.Value;
            string ngay = txtNgay.Value;
            string ghichu = txtghichu.Value;

            string gktttc = txtgktttc.Value;
            string gkquacty = txtgkquacty.Value;
            string gktkcm = txtgktkcm.Value;
            string gkthuechuyengia = txtgkthuechuyengia.Value;
            string gkptmautn = txtgkptmautn.Value;
            string gkptmaumt = txtgkptmaumt.Value;
            string gkhoachat = txtgkhoachat.Value;
            string gkluong = txtgkluong.Value;
            string gkchiphikhac = txtgkchiphikhac.Value;
            string gkthuemuangoaikhac = txtgkthuemuangoaikhac.Value;
            string gknvl = txtgknguyenvl.Value;

            string thtttc = txtthtttc.Value;
            string thquacty = txtthquacty.Value;
            string thtkcm = txtthtkcm.Value;
            string ththuechuyengia = txtththuechuyengia.Value;
            string thptmautn = txtthptmautn.Value;
            string thptmaumt = txtthptmaumt.Value;
            string thhoachat = txtthhoachat.Value;
            string thluong = txtthluong.Value;
            string thchiphikhac = txtthchiphikhac.Value;
            string ththuemuangoaikhac = txtththuemuangoaikhac.Value;
            string thnvl = txtthnguyenvl.Value;
            KhoanChiPhi kcp = new KhoanChiPhi();
            QLHD2013Entities db = new QLHD2013Entities();
            if (makcp > 0)
            {
                kcp = db.KhoanChiPhis.Where(q => q.MaKCP == makcp).FirstOrDefault();
                kcp.NoiDung = noidung;
                kcp.SoKCP = sokcp;
                kcp.GhiChu = ghichu;
                //if (parentid > 0)
                //    kcp.ParentID = parentid;
                //else
                //    kcp.ParentID = null;
                if (!string.IsNullOrEmpty(ngay))
                    kcp.Ngay = Utilities.Utilities.ConvertToDateTime(ngay);
                else
                    kcp.Ngay = DateTime.Now;

                if (!string.IsNullOrEmpty(giatri))
                    kcp.GiaTri = decimal.Parse(giatri);
                else
                    kcp.GiaTri = 0;

                if (!string.IsNullOrEmpty(gktttc))
                    kcp.CPGK_TTTC = decimal.Parse(gktttc);
                else
                    kcp.CPGK_TTTC = 0;

                if (!string.IsNullOrEmpty(gkquacty))
                    kcp.CPGK_QuaCTy = decimal.Parse(gkquacty);
                else
                    kcp.CPGK_QuaCTy = 0;

                if (!string.IsNullOrEmpty(gktkcm))
                    kcp.CPGK_TKCM = decimal.Parse(gktkcm);
                else
                    kcp.CPGK_TKCM = 0;

                if (!string.IsNullOrEmpty(gkthuechuyengia))
                    kcp.CPGK_ThueChuyenGia = decimal.Parse(gkthuechuyengia);
                else
                    kcp.CPGK_ThueChuyenGia = 0;

                if (!string.IsNullOrEmpty(gkptmautn))
                    kcp.CPGK_PTMauTN = decimal.Parse(gkptmautn);
                else
                    kcp.CPGK_PTMauTN = 0;

                if (!string.IsNullOrEmpty(gkptmaumt))
                    kcp.CPGK_PTMauMT = decimal.Parse(gkptmaumt);
                else
                    kcp.CPGK_PTMauMT = 0;

                if (!string.IsNullOrEmpty(gkhoachat))
                    kcp.CPGK_HoaChat = decimal.Parse(gkhoachat);
                else
                    kcp.CPGK_HoaChat = 0;

                if (!string.IsNullOrEmpty(gkluong))
                    kcp.CPGK_Luong = decimal.Parse(gkluong);
                else
                    kcp.CPGK_Luong = 0;

                if (!string.IsNullOrEmpty(gkchiphikhac))
                    kcp.CPGK_CPK = decimal.Parse(gkchiphikhac);
                else
                    kcp.CPGK_CPK = 0;

                if (!string.IsNullOrEmpty(gkthuemuangoaikhac))
                    kcp.CPGK_Thuemuangoai = decimal.Parse(gkthuemuangoaikhac);
                else
                    kcp.CPGK_Thuemuangoai = 0;

                if (!string.IsNullOrEmpty(gknvl))
                    kcp.CPGK_NVL = decimal.Parse(gknvl);
                else
                    kcp.CPGK_NVL = 0;

                if (!string.IsNullOrEmpty(thtttc))
                    kcp.CPTH_TTTC = decimal.Parse(thtttc);
                else
                    kcp.CPTH_TTTC = 0;

                if (!string.IsNullOrEmpty(thquacty))
                    kcp.CPTH_QuaCty = decimal.Parse(thquacty);
                else
                    kcp.CPTH_QuaCty = 0;

                if (!string.IsNullOrEmpty(thtkcm))
                    kcp.CPTH_TKCM = decimal.Parse(thtkcm);
                else
                    kcp.CPTH_TKCM = 0;

                if (!string.IsNullOrEmpty(ththuechuyengia))
                    kcp.CPTH_ThueChuyenGia = decimal.Parse(ththuechuyengia);
                else
                    kcp.CPTH_ThueChuyenGia = 0;

                if (!string.IsNullOrEmpty(thptmautn))
                    kcp.CPTH_PTMauTN = decimal.Parse(thptmautn);
                else
                    kcp.CPTH_PTMauTN = 0;

                if (!string.IsNullOrEmpty(thptmaumt))
                    kcp.CPTH_PTMauMT = decimal.Parse(thptmaumt);
                else
                    kcp.CPTH_PTMauMT = 0;

                if (!string.IsNullOrEmpty(thhoachat))
                    kcp.CPTH_HoaChat = decimal.Parse(thhoachat);
                else
                    kcp.CPTH_HoaChat = 0;

                if (!string.IsNullOrEmpty(thluong))
                    kcp.CPTH_Luong = decimal.Parse(thluong);
                else
                    kcp.CPTH_Luong = 0;

                if (!string.IsNullOrEmpty(thchiphikhac))
                    kcp.CPTH_CPK = decimal.Parse(thchiphikhac);
                else
                    kcp.CPTH_CPK = 0;


                if (!string.IsNullOrEmpty(ththuemuangoaikhac))
                    kcp.CPTH_Thuemuangoai = decimal.Parse(ththuemuangoaikhac);
                else
                    kcp.CPTH_Thuemuangoai = 0;

                if (!string.IsNullOrEmpty(thnvl))
                    kcp.CPTH_NVL = decimal.Parse(thnvl);
                else
                    kcp.CPTH_NVL = 0;
            }
            else
            {
                kcp.NoiDung = noidung;
                kcp.SoKCP = sokcp;
                kcp.MaHD = mahd;
                kcp.GhiChu = ghichu;
                kcp.NgayTao = DateTime.Now;
                if (parentid > 0)
                    kcp.ParentID = parentid;
                else
                    kcp.ParentID = null;
                if (!string.IsNullOrEmpty(ngay))
                    kcp.Ngay = Utilities.Utilities.ConvertToDateTime(ngay);
                else
                    kcp.Ngay = DateTime.Now;

                if (!string.IsNullOrEmpty(giatri))
                    kcp.GiaTri = decimal.Parse(giatri);
                else
                    kcp.GiaTri = 0;

                if (!string.IsNullOrEmpty(gktttc))
                    kcp.CPGK_TTTC = decimal.Parse(gktttc);
                else
                    kcp.CPGK_TTTC = 0;

                if (!string.IsNullOrEmpty(gkquacty))
                    kcp.CPGK_QuaCTy = decimal.Parse(gkquacty);
                else
                    kcp.CPGK_QuaCTy = 0;

                if (!string.IsNullOrEmpty(gktkcm))
                    kcp.CPGK_TKCM = decimal.Parse(gktkcm);
                else
                    kcp.CPGK_TKCM = 0;

                if (!string.IsNullOrEmpty(gkthuechuyengia))
                    kcp.CPGK_ThueChuyenGia = decimal.Parse(gkthuechuyengia);
                else
                    kcp.CPGK_ThueChuyenGia = 0;

                if (!string.IsNullOrEmpty(gkptmautn))
                    kcp.CPGK_PTMauTN = decimal.Parse(gkptmautn);
                else
                    kcp.CPGK_PTMauTN = 0;

                if (!string.IsNullOrEmpty(gkptmaumt))
                    kcp.CPGK_PTMauMT = decimal.Parse(gkptmaumt);
                else
                    kcp.CPGK_PTMauMT = 0;

                if (!string.IsNullOrEmpty(gkhoachat))
                    kcp.CPGK_HoaChat = decimal.Parse(gkhoachat);
                else
                    kcp.CPGK_HoaChat = 0;

                if (!string.IsNullOrEmpty(gkluong))
                    kcp.CPGK_Luong = decimal.Parse(gkluong);
                else
                    kcp.CPGK_Luong = 0;

                if (!string.IsNullOrEmpty(gkchiphikhac))
                    kcp.CPGK_CPK = decimal.Parse(gkchiphikhac);
                else
                    kcp.CPGK_CPK = 0;
                if (!string.IsNullOrEmpty(gkthuemuangoaikhac))
                    kcp.CPGK_Thuemuangoai = decimal.Parse(gkthuemuangoaikhac);
                else
                    kcp.CPGK_Thuemuangoai = 0;

                if (!string.IsNullOrEmpty(gknvl))
                    kcp.CPGK_NVL = decimal.Parse(gknvl);
                else
                    kcp.CPGK_NVL = 0;


                if (!string.IsNullOrEmpty(thtttc))
                    kcp.CPTH_TTTC = decimal.Parse(thtttc);
                else
                    kcp.CPTH_TTTC = 0;

                if (!string.IsNullOrEmpty(thquacty))
                    kcp.CPTH_QuaCty = decimal.Parse(thquacty);
                else
                    kcp.CPTH_QuaCty = 0;

                if (!string.IsNullOrEmpty(thtkcm))
                    kcp.CPTH_TKCM = decimal.Parse(thtkcm);
                else
                    kcp.CPTH_TKCM = 0;

                if (!string.IsNullOrEmpty(ththuechuyengia))
                    kcp.CPTH_ThueChuyenGia = decimal.Parse(ththuechuyengia);
                else
                    kcp.CPTH_ThueChuyenGia = 0;

                if (!string.IsNullOrEmpty(thptmautn))
                    kcp.CPTH_PTMauTN = decimal.Parse(thptmautn);
                else
                    kcp.CPTH_PTMauTN = 0;

                if (!string.IsNullOrEmpty(thptmaumt))
                    kcp.CPTH_PTMauMT = decimal.Parse(thptmaumt);
                else
                    kcp.CPTH_PTMauMT = 0;

                if (!string.IsNullOrEmpty(thhoachat))
                    kcp.CPTH_HoaChat = decimal.Parse(thhoachat);
                else
                    kcp.CPTH_HoaChat = 0;

                if (!string.IsNullOrEmpty(thluong))
                    kcp.CPTH_Luong = decimal.Parse(thluong);
                else
                    kcp.CPTH_Luong = 0;

                if (!string.IsNullOrEmpty(thchiphikhac))
                    kcp.CPTH_CPK = decimal.Parse(thchiphikhac);
                else
                    kcp.CPTH_CPK = 0;

                if (!string.IsNullOrEmpty(ththuemuangoaikhac))
                    kcp.CPTH_Thuemuangoai = decimal.Parse(ththuemuangoaikhac);
                else
                    kcp.CPTH_Thuemuangoai = 0;

                if (!string.IsNullOrEmpty(thnvl))
                    kcp.CPTH_NVL = decimal.Parse(thnvl);
                else
                    kcp.CPTH_NVL = 0;

                kcp.TrangThai = true;
                db.AddToKhoanChiPhis(kcp);
            }
            db.SaveChanges();

            QLHD.Models.KhoanChiPhi pkcp;

            if (kcp.KhoanChiPhi2 != null)
            {
                pkcp = kcp.KhoanChiPhi2;
            }
            else
            {
                pkcp = kcp;
            }

            if (pkcp.KhoanChiPhi1 != null && pkcp.KhoanChiPhi1.Count() > 0)
            {
                decimal? cpth = pkcp.KhoanChiPhi1.Sum(p => p.CPTH_CPK + p.CPTH_HoaChat + p.CPTH_Luong + p.CPTH_NVL + p.CPTH_PTMauMT + p.CPTH_PTMauTN + p.CPTH_QuaCty + p.CPTH_ThueChuyenGia + p.CPTH_Thuemuangoai + p.CPTH_TKCM + p.CPTH_TTTC);
                decimal? cpgk = pkcp.KhoanChiPhi1.Sum(p => p.CPGK_CPK + p.CPGK_HoaChat + p.CPGK_Luong + p.CPGK_NVL + p.CPGK_PTMauMT + p.CPGK_PTMauTN + p.CPGK_QuaCTy + p.CPGK_ThueChuyenGia + p.CPGK_Thuemuangoai + p.CPGK_TKCM + p.CPGK_TTTC);

                pkcp.CPGK_Total = cpgk;
                pkcp.CPTH_Total = cpth;
                db.SaveChanges();
            }

            if (kcp.HopDong.HopDong3 != null)
            {
                Utilities.Utilities.UpdateSearchContent(kcp.HopDong.HopDong3, db);
            }
            else
            {
                Utilities.Utilities.UpdateSearchContent(kcp.HopDong, db);
            }
            //db.SaveChanges();
            Response.Redirect(Request.UrlReferrer.ToString());
        }
    }
}